package cn.gcyl.module.biz.controller.admin.thirdpartyinformation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.thirdpartyinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import cn.gcyl.module.biz.service.thirdpartyinformation.ThirdPartyInformationService;

@Tag(name = "管理后台 - 第三方信息管理")
@RestController
@RequestMapping("/biz/third-party-information")
@Validated
public class ThirdPartyInformationController {

    @Resource
    private ThirdPartyInformationService thirdPartyInformationService;

//    @PostMapping("/create")
//    @Operation(summary = "创建第三方信息管理")
//    @PreAuthorize("@ss.hasPermission('biz:third-party-information:create')")
//    public CommonResult<Long> createThirdPartyInformation(@Valid @RequestBody ThirdPartyInformationSaveReqVO createReqVO) {
//        return success(thirdPartyInformationService.createThirdPartyInformation(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新第三方信息管理")
//    @PreAuthorize("@ss.hasPermission('biz:third-party-information:update')")
//    public CommonResult<Boolean> updateThirdPartyInformation(@Valid @RequestBody ThirdPartyInformationSaveReqVO updateReqVO) {
//        thirdPartyInformationService.updateThirdPartyInformation(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除第三方信息管理")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:third-party-information:delete')")
//    public CommonResult<Boolean> deleteThirdPartyInformation(@RequestParam("id") Long id) {
//        thirdPartyInformationService.deleteThirdPartyInformation(id);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除第三方信息管理")
//                @PreAuthorize("@ss.hasPermission('biz:third-party-information:delete')")
//    public CommonResult<Boolean> deleteThirdPartyInformationList(@RequestParam("ids") List<Long> ids) {
//        thirdPartyInformationService.deleteThirdPartyInformationListByIds(ids);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得第三方信息管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:third-party-information:query')")
    public CommonResult<ThirdPartyInformationRespVO> getThirdPartyInformation(@RequestParam("id") Long id) {
        ThirdPartyInformationDO thirdPartyInformation = thirdPartyInformationService.getThirdPartyInformation(id);
        return success(BeanUtils.toBean(thirdPartyInformation, ThirdPartyInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得第三方信息管理分页")
//    @PreAuthorize("@ss.hasPermission('biz:third-party-information:query')")
    public CommonResult<PageResult<ThirdPartyInformationRespVO>> getThirdPartyInformationPage(@Valid ThirdPartyInformationPageReqVO pageReqVO) {
        PageResult<ThirdPartyInformationDO> pageResult = thirdPartyInformationService.getThirdPartyInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ThirdPartyInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出第三方信息管理 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportThirdPartyInformationExcel(@Valid ThirdPartyInformationPageReqVO pageReqVO,
                                                 HttpServletResponse response) throws IOException {
        List<ThirdPartyInformationDO> list = thirdPartyInformationService.getThirdPartyInformationPage(pageReqVO).getList();

        // 转换前处理数据 - 截断description字段
        List<ThirdPartyInformationRespVO> exportList = BeanUtils.toBean(list, ThirdPartyInformationRespVO.class);
        exportList.forEach(vo -> {
            if (vo.getDescription() != null && vo.getDescription().length() > 32700) {
                vo.setDescription(vo.getDescription().substring(0, 32700) + "...[内容过长已截断]");
            }
        });

        // 导出 Excel
        ExcelUtils.write(response, "第三方信息管理.xls", "数据", ThirdPartyInformationRespVO.class, exportList);
    }

}